<template>
  <el-dialog
    class="assign-role"
    title="分配角色 "
    :visible="showRoleDialog"
    width="40%"
    @close="close"
    @open="open"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList" v-loading="loading">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>

    </el-checkbox-group>
    <template #footer>
      <el-button size="small" @click="close">取消</el-button>
      <el-button type="primary" size="small" @click="assign">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetail } from '@/api/user'
import { assignRole } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkList: [],
      roleList: [],
      loading: false
    }
  },

  methods: {
    close() {
      this.$emit('update:showRoleDialog', false)
    },
    async getRoleList() {
      const data = await getRoleList(1, 100)
      //   console.log(data)
      this.roleList = data.rows
    },
    async getUserDetail() {
      const data = await getUserDetail(this.userId)
      //   console.log(data)
      this.checkList = data.roleIds
    },
    async open() {
      this.loading = true
      await Promise.all([this.getRoleList(), this.getUserDetail()])
      this.loading = false
    },
    async assign() {
      await assignRole(this.userId, this.checkList)
      this.$message.success('分配角色成功')
      this.close()
    }
  }
}
</script>
